﻿using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace 碧蓝肝帝
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            radioButton1.Checked = true;
            StartPosition = FormStartPosition.CenterScreen;

        }

        /// <summary>
        /// 任务类型
        /// </summary>
        public string Rw = "";                            
        /// <summary>
        /// 困难模式
        /// </summary>
        public bool Hard = false;                            
        /// <summary>
        /// 出击次数
        /// </summary>
        public int Cn = 1;
        /// <summary>
        /// 任务目标-1
        /// </summary>
        public string Rw_target = "";
        /// <summary>
        /// //任务目标-2
        /// </summary>
        public string Rw_target2 = "";
        /// <summary>
        /// 程序地址
        /// </summary>
        public string dir = AppDomain.CurrentDomain.BaseDirectory;
        public Dictionary<string, string> task_dic = new Dictionary<string, string>();
        /// <summary>
        /// 0 普通，1活动，2档案
        /// </summary>
        public int sign_number = 0;

        /// <summary>
        /// 线程字典
        /// </summary>
        public Dictionary<int, Thread> thread_dic = new Dictionary<int, Thread>();
        /// <summary>
        /// 线程id
        /// </summary>
        public int thread_id = 0;
        public bool printf_text = true;

        /// <summary>
        /// 停止按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button2_Click(object sender, EventArgs e)
        {
            printf_text = false;
            if(thread_dic.ContainsKey(thread_id))
            {
                thread_dic[thread_id].Abort();
                thread_dic.Remove(thread_id);
            }
            
        }

        /// <summary>
        /// 开始按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            Point point = new Point();        
            //开始           
            if(radioButton1.Checked)
            {
                //普通
                Rw_target = comboBox4.Text;
                if(Rw_target != "")
                {
                    sign_number = 0;
                    task_dic = Other.Readsetting(dir + @"\Data\Common.txt");
                    task_dic.TryGetValue(Rw_target, out string st1);
                    string[] xp = st1.Split(',');
                    try
                    {
                        point.X = Convert.ToInt32(xp[0]);
                        point.Y = Convert.ToInt32(xp[1]);
                        Play(point);
                    }
                    catch
                    {
                        MessageBox.Show("[A1]坐标设置错误");
                    }
                    
                }
                else
                {
                    MessageBox.Show("请先选择目标");
                }
            }
            else if(radioButton2.Checked)
            {
                //活动
                Rw_target = comboBox1.Text;
                if(Rw_target != "")
                {
                    sign_number = 1;
                    task_dic = Other.Readsetting(dir + @"\Data\special_files.txt");
                    task_dic.TryGetValue(Rw_target, out string st1);
                    string[] xp = st1.Split(',');
                    try
                    {
                        point.X = Convert.ToInt32(xp[0]);
                        point.Y = Convert.ToInt32(xp[1]);
                        Play(point);
                    }
                    catch
                    {
                        MessageBox.Show("[]坐标设置错误");
                    }
                }
                else
                {
                    MessageBox.Show("请先选择目标");
                }
            }
            else if(radioButton3.Checked)
            {
                //档案
                Rw_target = comboBox2.Text;
                Rw_target2 = comboBox3.Text;
                if(Rw_target != "" && Rw_target2 != "")
                {
                    sign_number = 2;
                    textBox2.AppendText("即将开始，请准备" + System.Environment.NewLine);
                    Other.Sleep(2);
                    point = Ex_and_Sp.Ex(Rw_target, Rw_target2);
                    Play(point);
                }
                else
                {
                    MessageBox.Show("请先选择目标");
                }

            }
            else if(radioButton4.Checked)
            {
                //演习
                Rw_target = "YX";
                MessageBox.Show("什么也没有发生");
            }
            
        }

            

        
       
        private void numericUpDown3_ValueChanged(object sender, EventArgs e)
        {
            //获取出击次数
            Cn = (int)numericUpDown3.Value;
        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            //困难模式
            Hard = checkBox1.Checked;
        }
        /// <summary>
        /// 调用物体检测SDK
        /// </summary>
        private void Start()
        {
            Process process = new Process();
            process.StartInfo.UseShellExecute = false;
            process.StartInfo.CreateNoWindow = true;
            string rootDir = "SDK根路径";
            string serialNum = "您的序列号";
            process.StartInfo.FileName = string.Format("{0}\\python37\\python.bat", rootDir);
            process.StartInfo.Arguments = string.Format("{1} 127.0.0.1 24401",
                                                        rootDir, serialNum);
            process.Start();
        }

        private void Play(Point point )
        {           
            Task_map task = new Task_map();
            printf_text = true;
            task.Point = point;
            task.task_num = Cn;
            task.hard = Hard;
            task.work_sign = sign_number;
            progressBar1.Maximum = Cn;  //进度条
            textBox2.AppendText("[*]请切换界面，准备开始" + System.Environment.NewLine);
            Other.Sleep(3);
            Thread_play play = new Thread_play
            {
                Task_Map = task
            };
            Thread thread_1 = new Thread(play.Thread_main);
            thread_1.Start();
            thread_id = play.Thread_id;
            thread_dic.Clear();
            thread_dic.Add(thread_id, thread_1);
            while(printf_text)
            {
                Application.DoEvents();
                if(play.Message_queue.Count > 0)
                {
                    if(play.Playnumber_queue.Count > 0)
                    {
                        progressBar1.Value = (int)play.Playnumber_queue.Dequeue();
                    }
                    string str = (string)play.Message_queue.Dequeue();
                    textBox2.AppendText(str);
                    Text_save.Save(str);
                }
                if(play.State_queue.Count > 0)
                {
                    break;
                }
            }
            File.WriteAllText(AppDomain.CurrentDomain.BaseDirectory + @"\Data\过程记录.txt", textBox2.Text);
            textBox2.AppendText("[!]线程正常结束" + System.Environment.NewLine);

        }

        private void Form1_Load(object sender, EventArgs e)
        {
            
        }

        /// <summary>
        /// 显示日志
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button3_Click(object sender, EventArgs e)
        {
            Form2 form2 = new Form2
            {
                save_class = 0,
                StartPosition = FormStartPosition.CenterParent
        };
            form2.Append_log();           
            form2.ShowDialog();
            
        }

        /// <summary>
        /// 设置
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button4_Click(object sender, EventArgs e)
        {
            Form3 form3 = new Form3
            {
                StartPosition = FormStartPosition.CenterParent
            };
            form3.Show();
            
        }

        /// <summary>
        /// 退出程序时，关闭所有线程
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            printf_text = false;
            if(thread_dic.ContainsKey(thread_id))
            {
                thread_dic[thread_id].Abort();
                thread_dic.Remove(thread_id);
            }
        }
    }



    /// <summary>
    /// 多线程，子类化
    /// </summary>
    internal class Thread_play
    {
        public int Thread_id { get; set; }
        public Task_map Task_Map { get; set; }

        public Queue Message_queue = new Queue();
        public Queue Playnumber_queue = new Queue();
        public Queue State_queue = new Queue();

        public void Thread_main()
        {
            Work work = new Work();
            Thread_id = Thread.CurrentThread.ManagedThreadId;         
            Message_queue = work.Message_queue;
            Playnumber_queue = work.Playnumber_queue;
            State_queue = work.State_queue;
            work.Playing(Task_Map);

        }
    }

    /// <summary>
    /// 日志保存
    /// </summary>
    internal class Text_save
    {
        public static ArrayList Log_list = new ArrayList();

        public static void Save(string str)
        {
            Log_list.Add(str);
        }
    }
}


